﻿using System;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public class SearchPopupWindow : PopupWindowContent
    {
        private string search = "";
        private List<PopupItem> options = new List<PopupItem>();
        private PopupItem selected;
        private Vector2 scrollPos;
        private Action<PopupItem> onSelected;

        private static readonly GUIStyle selectedButton;
        private static readonly GUIStyle defaultButton;

        static SearchPopupWindow()
        {
            selectedButton = new GUIStyle(GUI.skin.label);
            selectedButton.normal.textColor = Color.white;
            selectedButton.normal.background = MakeTexture(2, 2, new Color(0.2f, 0.4f, 0.6f));
            defaultButton = new(GUI.skin.label);

            Texture2D MakeTexture(int width, int height, Color color)
            {
                Color[] pixels = new Color[width * height];
                for (int i = 0; i < pixels.Length; i++)
                {
                    pixels[i] = color;
                }

                Texture2D texture = new Texture2D(width, height);
                texture.SetPixels(pixels);
                texture.Apply();

                return texture;
            }
        }

        public override void OnGUI(Rect rect)
        {
            this.search = EditorGUILayout.TextField(this.search);

            var rList = this.options.FindAll(x => x.display.Contains(search) || string.IsNullOrEmpty(this.search));
            EditorGUILayout.BeginVertical();
            this.scrollPos = EditorGUILayout.BeginScrollView(this.scrollPos);
            GUI.color = Color.white;
            for (int i = 0; i < rList.Count; i++)
            {
                var rValue = rList[i];
                bool bSelected = rValue == this.selected;
                if (GUILayout.Button(rValue.display, bSelected ? selectedButton : defaultButton))
                {
                    this.OnSelected(rValue);
                }
                EditorGUILayout.Space(1);
            }
            EditorGUILayout.EndScrollView();
            EditorGUILayout.EndVertical();
        }

        private void OnSelected(PopupItem value)
        {
            this.onSelected?.Invoke(value);
            this.editorWindow.Close();
        }

        public class PopupItem
        {
            public string display;
            public long numValue;

            // 0 int-str 1 str
            public int itemType;
        }

        public static void Create(Rect rect, List<string> rStrList, string selected, Action<string> onSelected)
        {
            var rItemList = rStrList.Select((x) => new SearchPopupWindow.PopupItem() { display = x, itemType = 1 }).ToList();
            SearchPopupWindow window = new SearchPopupWindow()
            {
                options = rItemList,
                selected = rItemList.Find((x) => x.display == selected),
                onSelected = (x) => onSelected?.Invoke(x.display)
            };
            rect.x -= rect.width;
            PopupWindow.Show(rect, window);
        }

        public static void Create(Rect rect, List<(long, string)> rValueList, long nSelected, Action<long> onSelected)
        {
            var rItemList = rValueList.Select((x) => new SearchPopupWindow.PopupItem()
                { display = x.Item2, numValue = x.Item1, itemType = 0 }).ToList();
            SearchPopupWindow window = new SearchPopupWindow()
            {
                options = rItemList,
                selected = rItemList.Find((x) => x.numValue == nSelected),
                onSelected = (x) => onSelected?.Invoke(x.numValue),
            };
            rect.x -= rect.width;
            PopupWindow.Show(rect, window);
        }

        public static void Create(Rect rect, List<PopupItem> rItemList, PopupItem rSelected, Action<PopupItem> onSelected)
        {
            SearchPopupWindow window = new SearchPopupWindow()
            {
                options = rItemList,
                selected = rSelected,
                onSelected = (x) => onSelected?.Invoke(x),
            };
            rect.x -= rect.width;
            PopupWindow.Show(rect, window);
        }
    }
}